package biz

import (
	"New-lesson-part2/custom_error"
	"New-lesson-part2/internal"
	"New-lesson-part2/model"
	"New-lesson-part2/proto/pb"
	"context"
	"errors"
	"github.com/golang/protobuf/ptypes/empty"
	"gorm.io/gorm"
)

func (p ProductServer) AdvertiseList(ctx context.Context, empty *empty.Empty) (*pb.AdvertisesRes, error) {
	var adList []model.Advertise                     //数据库查询出来的广告列表
	var advertiseItemResList []*pb.AdvertisesItemRes //每一个广告的所有字段信息.构成一个广告主体结构
	var advertiseRes pb.AdvertisesRes                //最终返回的广告总数和广告列表信息
	r := internal.DB.Find(&adList)
	if r.RowsAffected < 1 {
		return nil, errors.New(custom_error.AdvertiseNotExist)
	}
	if r.Error != nil {
		return nil, errors.New(custom_error.AdvertiseDatabaseQueryError)
	}
	for _, i := range adList {
		advertiseItemResList = append(advertiseItemResList, ConvertAdvertises2pb(i))
	}

	var count int64
	res := internal.DB.Model(&adList).Count(&count)
	if count < 1 {
		return nil, errors.New(custom_error.AdvertiseNotExist)
	}
	if res.Error != nil {
		return nil, errors.New(custom_error.AdvertiseDatabaseQueryError)
	}
	advertiseRes.Total = int32(count)
	advertiseRes.AdvertisesList = advertiseItemResList
	return &advertiseRes, nil
}

func (p ProductServer) CreateAdvertises(ctx context.Context, req *pb.AdvertisesReq) (*pb.AdvertisesItemRes, error) {
	var advertise model.Advertise
	var res *pb.AdvertisesItemRes
	advertise.ID = req.Id
	advertise.Index = req.Index
	advertise.Url = req.Url
	advertise.Image = req.Image
	r := internal.DB.Save(&advertise)
	if r.RowsAffected < 1 {
		return nil, errors.New(custom_error.AdvertiseFailedToCreateData)
	}
	if r.Error != nil {
		return nil, errors.New(custom_error.AdvertiseSystemError)
	}
	res = ConvertAdvertises2pb(advertise)

	return res, nil
}

func (p ProductServer) DeleteAdvertises(ctx context.Context, req *pb.DeleteAdvertisesReq) (*empty.Empty, error) {
	var ad model.Advertise
	r := internal.DB.First(&ad, req.Id)
	info, err := CheckReturnInfo(r)

	if req.Id != 0 {
		r := internal.DB.Delete(&model.Advertise{}, req.Id)
		if r.RowsAffected == 0 {
			return nil, errors.New(custom_error.DeleteAdvertiseIsNotExist)
		}
	} else {
		return nil, errors.New(custom_error.ParameterError)
	}
	return info, err

}

func (p ProductServer) UpdateAdvertises(ctx context.Context, req *pb.AdvertisesReq) (*empty.Empty, error) {
	var ad model.Advertise
	r := internal.DB.First(&ad, req.Id)
	if r.RowsAffected < 1 {
		return nil, errors.New(custom_error.AdvertiseNotExist)
	}
	if r.Error != nil {
		return nil, errors.New(custom_error.AdvertiseSystemError)
	}
	if req.Id != 0 {
		ad.ID = req.Id
	}
	if req.Url != "" {
		ad.Url = req.Url
	}
	if req.Sort != 0 {
		ad.Sort = req.Sort
	}
	if req.Image != "" {
		ad.Image = req.Image
	}
	if req.Index != 0 {
		ad.Index = req.Index
	}
	r = internal.DB.Model(&model.Advertise{}).Save(&ad)
	if r.Error != nil {
		return nil, errors.New(custom_error.AdvertiseSystemError)
	}
	if r.RowsAffected < 1 {
		return nil, errors.New(custom_error.AdvertiseUpdateFailed)
	}
	return &empty.Empty{}, nil
}

func ConvertAdvertises2pb(req model.Advertise) *pb.AdvertisesItemRes {
	return &pb.AdvertisesItemRes{
		Id:    req.ID,
		Index: req.Index,
		Image: req.Image,
		Url:   req.Url,
		Sort:  req.Sort,
	}
}

func CheckReturnInfo(r *gorm.DB) (*empty.Empty, error) {
	if r.Error != nil {
		return nil, errors.New(custom_error.AdvertiseSystemError)
	}
	if r.RowsAffected < 1 {
		return nil, errors.New(custom_error.AdvertiseNotExist)
	}
	return &empty.Empty{}, nil
}
